﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;

using System.Linq;

using ProjectBase.Core;

using $prjsn$.Common.Business.Services;
using $prjsn$.Common.Data.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services
{
    public class TranslatorFacade
        : BaseFacade<Translator, int, TranslatorFilter, ITranslatorDao>, ITranslatorFacade
    {
        #region IBaseFacade<T> override - FACTORY METHOD
        public override Translator CreateNew()
        {
            var entity = new Translator
            {
                Key = string.Empty,
                Area = string.Empty
            };
            return entity;
        }
        #endregion IBaseFacade<T> override - FACTORY METHOD

        #region Bind
        public override Translator BindEntity(Translator entity, IValueContainer valueContainer
            , string prefix = null, string[] includeProperties = null, string[] excludeProperties = null)
        {
            var result = base.BindEntity(entity, valueContainer, prefix, includeProperties, excludeProperties);
            
            BindPhrases(entity, valueContainer);

            return result;
        }
        public override TranslatorFilter BindFilter(TranslatorFilter filter, IValueContainer valueContainer
            , string prefix = null, string[] includeProperties = null, string[] excludeProperties = null)
        {
            if (valueContainer.ContainsPrefix("SelectedLanguage"))
            {
                filter.SearchedLanguageName = valueContainer.GetValue("SelectedLanguage").AttemptedValue;
            }
            return base.BindFilter(filter, valueContainer, prefix, includeProperties, excludeProperties);
        }

        protected virtual bool BindPhrases(Translator entity, IValueContainer valueContainer)
        {
            foreach (var lang in LangFacade.GetAll())
            {
                if (valueContainer.ContainsPrefix(lang.LanguageName))
                {
                    entity.Translations[lang.LanguageName] = valueContainer.GetValue(lang.LanguageName).AttemptedValue;
                }
            }
            return true;
        }
        #endregion Bind

        #region constructor
        static TranslatorFacade()
        {
            ChangeHandler += Clear;
        }
        #endregion constructor

        #region ITranslatorFacade
        protected static IEnumerable<Translator> Translators { get; set; }

        public virtual IEnumerable<Translator> GetAll()
        {
            var translators = Translators;
            if (translators.IsNull())
            {
                translators = Dao.GetAll().Select(e => e.Clone() as Translator).ToList().AsReadOnly();
                Translators = translators;
            }
            return translators;
        }

        static void Clear(object sender, EventArgs e)
        {
            Translators = null;
        }
        #endregion ITranslatorFacade

        #region properties
        public virtual ILangFacade LangFacade { protected get; set; }
        #endregion properties
    }
}